import java.rmi.Naming;
import java.rmi.RemoteException;
import java.io.BufferedReader;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.util.HashMap;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;
import java.util.Scanner;

public class XCliente {

	public static void main(String[] args) {
		try {
			X c = (X) Naming.lookup("rmi://localhost:1099/ServicioX");
			String line;
			Scanner in = new Scanner(System.in);
			while (true) {
				System.out.println("Opcion 1: Nova Reserva");
				System.out.println("Opcion 2: Llistar Transaccions");
				System.out.println("Opcion 0: Sortir");
				line = in.nextLine();
				int option = Integer.parseInt(line);
				switch (option) {
				case 1:
					Map<String, String> values = new HashMap<String, String>();

					System.out.println("Insereix les dades del lloguer:");
					System.out
							.println("Model:(Economic/Semi-Luxe/Luxe/Limusina)");
					values.put("model", in.nextLine());
					System.out.println("Motor:(Gasolina/Diesel)");
					values.put("motor", in.nextLine());
					System.out.println("Quantitat: ");
					values.put("quantitat", in.nextLine());
					System.out.println("Dies: ");
					values.put("dies", in.nextLine());
					System.out.println("Descompte: *.*");
					values.put("descompte", in.nextLine());
					System.out.println("Guardant Reserva...");
					double preuFinal = c.addEntry(values);
					System.out.println("");
					System.out.println("Preu Final: " + preuFinal);
					break;
				case 2:
					String username,
					password;
					System.out.println("Usuari: ");
					username = in.nextLine();
					System.out.println("Contrasenya: ");
					password = in.nextLine();
					System.out.println("");
					System.out.println("Llistat de transaccions:");

					ArrayList<Map<String, String>> transaccions = c
							.getTransactions(username, password);
					if (transaccions != null) {
						for (Map<String, String> t : transaccions) {
							System.out.println("-- Model: " + t.get("model")
									+ " " + "Motor: " + t.get("motor") + " "
									+ "Quantitat: " + t.get("quantitat") + " "
									+ "Dies: " + t.get("dies") + " "
									+ "Descompte: " + t.get("descompte") + " "
									+ "Preu Final: " + t.get("preufinal"));
						}
					} else {
						System.out
								.println("Error: Usuari o Password incorrectes");
					}
					break;
				case 0:
					System.exit(0);
				default:
					System.out.print("Opcio Incorrecte");
					break;
				}
			}
		}

		catch (MalformedURLException murle) {
			System.out.println("MalformedURLException: " + murle);
		} catch (RemoteException re) {
			System.out.println("RemoteException: " + re);
		} catch (NotBoundException nbe) {
			System.out.println("NotBoundException: " + nbe);
		} catch (java.lang.ArithmeticException ae) {
			System.out.println("java.lang.ArithmeticException: " + ae);
		}
	}
}
